package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.IndicatorSystemTaskDto;
import com.manage.platform.dto.query.IndicatorSystemPartDto;
import com.manage.platform.dto.query.IndicatorSystemTaskQuery;
import com.manage.platform.entity.IndicatorSystemEntity;
import com.manage.platform.mapper.IndicatorSystemMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.utils.TreeListIndicatorSystemUtil;
import com.manage.platform.utils.TreeListTaskTreeUtil;
import com.manage.platform.vo.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/9/23
 */
@AllArgsConstructor
@Service
public class IndicatorSystemImpl extends ServiceImpl<IndicatorSystemMapper, IndicatorSystemEntity> {

    private final IndicatorSystemTaskServiceImpl indicatorSystemTaskServiceImpl;

    public List<CommonVo> getIndicatorSystemOneList() {
        return this.baseMapper.getIndicatorSystemOneList();
    }

    public List<IndicatorSystemTreeVo> getIndicatorSystemTree(Long id, String name) {
        List<IndicatorSystemTreeVo> list = this.baseMapper.getIndicatorSystemTree(id, name);
        //构建树形
        return TreeListIndicatorSystemUtil.buildMenuTreeList(list);
    }

    public List<IndicatorSystemTreeOneVo> getIndicatorSystemTreeOne(Long id) {
        List<IndicatorSystemTreeOneVo> list = this.baseMapper.getIndicatorSystemTreeOne(id);
        //构建树形
        return TreeListIndicatorSystemUtil.buildMenuTreeListOne(list);
    }


    public IndicatorSystemVo getIndicatorSystemDetails(Long id) {
        return BeanUtil.copyProperties(getById(id), IndicatorSystemVo.class);
    }

    public Boolean saveIndicatorSystemPart(IndicatorSystemPartDto dto) {
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        return save(BeanUtil.copyProperties(dto, IndicatorSystemEntity.class));
    }


    public Boolean updateIndicatorSystemPart(IndicatorSystemPartDto dto) {
        return updateById(BeanUtil.copyProperties(dto, IndicatorSystemEntity.class));
    }

    public Boolean deleteIndicatorSystem(Long id) {
        //删除关联数据
        return removeById(id);
    }

    public List<CommonVo> getIndicatorList(String name) {
        return this.baseMapper.getIndicatorList(name);
    }

    public List<IndicatorSystemTaskVo> getTaskList(IndicatorSystemTaskQuery query) {
//        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
//        List<IndicatorSystemTaskVo> list = this.baseMapper.getTaskList(query);
//        //树形
//        List<IndicatorSystemTaskVo> list1 = TreeListTaskTreeUtil.buildMenuTreeListTwo(list);
//        return RPage.ok(list1, query.getPage(), query.getLimit(), page.getTotal());

        List<IndicatorSystemTaskVo> list = this.baseMapper.getTaskList(query);
        //树形
        return TreeListTaskTreeUtil.buildMenuTreeListTwo(list);
    }

    public Boolean linkTask(IndicatorSystemTaskDto dto) {
        if (CollectionUtil.isNotEmpty(dto.getTaskId())) {
            return indicatorSystemTaskServiceImpl.linkTask(dto);
        }
        return true;
    }



}
